/**
 * Copyright (c) 2019, ShangHai YTO Co., Ltd.
 * All right reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF YTO
 * CO., LTD.  THE CONTENTS OF THIS FILE MAY NOT BE DISCLOSED
 * TO THIRD PARTIES, COPIED OR DUPLICATED IN ANY FORM, IN WHOLE OR IN PART,
 * WITHOUT THE PRIOR WRITTEN PERMISSION OF YTO
 * CO., LTD.
 */
package org.sc.xl.algorithm.exceldownload;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.Units;
import org.apache.poi.xssf.usermodel.XSSFColor;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: TODO 请在此添加描述
 * @reason: TODO ADD REASON(可选)
 * @author FXL
 *
 * @date 2024/10/15 19:48 
 * @since JDK 1.8
 */
public class Test extends AbstractCellStyleStrategy implements CellWriteHandler {


    public static void main(String[] args) throws IOException {
        //指定写出的excel文件
        String fileName = "C:\\Users\\admin\\Desktop\\demo.xlsx";
        //指定读取的图片文件
        String imagePath = "D:\\project\\self-pro\\tools\\src\\main\\java\\org\\sc\\xl\\exceldownload\\红色箭头.png";
        //如果插入多张
        List<IndexOverviewResult> list = new ArrayList<>();
        //图片对象
        String jsonStr = "[\n" +
                "    {\n" +
                "        \"indexName\": \"1、市占率\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"2、业务量(日均)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"万票\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"2、业务量(日均)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"差头部\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"3、利润(万元)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"财务\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"3.1、单票利润\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"财务\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"4、负毛利网点(万元)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"财务\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"数量\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"4、负毛利网点(万元)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"财务\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"金额\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"5、应收账款(万元)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"财务\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"金额\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"6、闲置场地(m2)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"财务\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"7、单票收入\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"8、直营客户\\r\\n(万票/日)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"业务量\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"8、直营客户\\r\\n(万票/日)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"单票毛利\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"9、腰部客户\\r\\n(万票/日)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"业务量\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"9、腰部客户\\r\\n(万票/日)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"占比\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"10、散单\\r\\n(万票/日)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"业务量\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"10、散单\\r\\n(万票/日)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"占比(占全省)\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"10、散单\\r\\n(万票/日)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"0散单驿站数\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"10、散单\\r\\n(万票/日)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"0散单业务员数\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"11、国际件\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"业务量(票)\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"11、国际件\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"专业市场推进\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"12、航空件业务量(票)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"13、圆准达业务量(票)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"14、同城特快(票)\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"15、货型结构\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"均重\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"15、货型结构\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"kg以上票数占比\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    },\n" +
                "    {\n" +
                "        \"indexName\": \"15、货型结构\",\n" +
                "        \"lastYearCompleted\": \"0\",\n" +
                "        \"moduleName\": \"营销\",\n" +
                "        \"month01\": \"231\",\n" +
                "        \"month02\": \"212\",\n" +
                "        \"month03\": \"212\",\n" +
                "        \"projectName\": \"3kg以上票数占比\",\n" +
                "        \"yearTargetGoal\": \"0\",\n" +
                "        \"yearToDate\": \"0\"\n" +
                "    }\n" +
                "]";
        List<IndexOverviewResult> indexOverviewResults = JSONArray.parseArray(jsonStr, IndexOverviewResult.class);

        EasyExcel.write(fileName).sheet().head(headList).registerWriteHandler(new Test(2, new int[]{1, 2}))
                .doWrite(indexOverviewResults);
    }


    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer columnIndex, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {

    }

    @Override
    public void afterCellDataConverted(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, CellData cellData, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
    }


    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        Sheet sheet = cell.getSheet();
        Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
        //设置样式
        CellStyle style = workbook.createCellStyle();
        if (isHead) {
            if (0 == cell.getRowIndex()) {
                // 设置字体
                Font font = workbook.createFont();
                //设置字体大小
                font.setFontHeightInPoints((short) 16);
                //字体加粗
                font.setBold(true);
                //设置字体名字
                font.setFontName("微软雅黑");
                //左右居中
                style.setAlignment(HorizontalAlignment.CENTER);
                //垂直居中
                style.setVerticalAlignment(VerticalAlignment.CENTER);
                //在样式用应用设置的字体;
                style.setFont(font);
                //设置自动换行;
                style.setWrapText(false);
                //设置第一列无边框
                if (0 == cell.getColumnIndex()) {
                    //设置第一列列宽
                    sheet.setColumnWidth(cell.getColumnIndex(), 300);
                    style.setBorderRight(BorderStyle.NONE);
                    style.setBorderLeft(BorderStyle.NONE);
                    style.setBorderTop(BorderStyle.NONE);
                    style.setBorderBottom(BorderStyle.NONE);
                } else {
                }
                cell.setCellStyle(style);
            }
            if (1 == cell.getRowIndex()) {
                // 设置字体
                Font font = workbook.createFont();
                //设置字体大小
                font.setFontHeightInPoints((short) 12);
                //字体加粗
                font.setBold(true);
                //设置字体名字
                font.setFontName("楷体");
                //左右居中
                style.setAlignment(HorizontalAlignment.CENTER);
                //垂直居中
                style.setVerticalAlignment(VerticalAlignment.CENTER);

                if (writeSheetHolder.getHead().size() - 1 == cell.getColumnIndex()) {
                    //设置最后一列有边框
                    style.setBorderRight(BorderStyle.DOUBLE);
                    style.setRightBorderColor(IndexedColors.BLACK.getIndex());
                    //设置字体大小
                    font.setFontHeightInPoints((short) 8);
                    sheet.setColumnWidth(cell.getColumnIndex(), 7000);
                } else if (1 == cell.getColumnIndex()) {
                    //设置第一列有边框
                    style.setBorderLeft(BorderStyle.DOUBLE);
                    sheet.setColumnWidth(cell.getColumnIndex(), 1500);
                } else if (2 == cell.getColumnIndex()) {
                    //设置第二列有边框
                    style.setBorderRight(BorderStyle.THIN);
                    sheet.setColumnWidth(cell.getColumnIndex(), 5000);
                } else if (3 == cell.getColumnIndex()) {
                    //设置第三列宽度
                    style.setBorderRight(BorderStyle.THIN);
                    sheet.setColumnWidth(cell.getColumnIndex(), 3000);
                } else {
                    style.setBorderRight(BorderStyle.THIN);
                }
                style.setBorderTop(BorderStyle.DOUBLE);
                style.setTopBorderColor(IndexedColors.BLACK.getIndex());
                style.setBorderBottom(BorderStyle.THIN);
                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                XSSFColor customColor = new XSSFColor(new java.awt.Color(128, 128, 128));
                style.setFillForegroundColor();
                //在样式用应用设置的字体;
                style.setFont(font);
                //设置自动换行;
                style.setWrapText(false);
                sheet.getRow(1).setHeight((short) 400);
                //设置第一列无边框
                if (0 == cell.getColumnIndex()) {
                    //设置第一列列宽
                    sheet.setColumnWidth(cell.getColumnIndex(), 300);
                    style.setBorderRight(BorderStyle.NONE);
                    style.setBorderLeft(BorderStyle.NONE);
                    style.setBorderTop(BorderStyle.NONE);
                    style.setBorderBottom(BorderStyle.NONE);
                    style.setFillPattern(FillPatternType.NO_FILL);
                    style.setFillForegroundColor(IndexedColors.WHITE.getIndex());
                } else {
                }
                cell.setCellStyle(style);
            }
        } else {
            // 设置字体
            Font font = workbook.createFont();
            //设置字体大小
            font.setFontHeightInPoints((short) 12);
            //设置字体名字
            font.setFontName("楷体");
            //左右居中
            style.setAlignment(HorizontalAlignment.CENTER);
            //垂直居中
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            //设置边框
            style.setBorderBottom(BorderStyle.THIN);
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);
            //设置边框
            if (1 == cell.getColumnIndex()) {
                style.setBorderLeft(BorderStyle.DOUBLE);
            }
            style.setBorderTop(BorderStyle.THIN);

            //在样式用应用设置的字体;
            style.setFont(font);
            //设置自动换行;
            style.setWrapText(false);
            //设置第一列无边框
            if (0 == cell.getColumnIndex()) {
                //设置第一列列宽
                sheet.setColumnWidth(cell.getColumnIndex(), 300);
                style.setBorderRight(BorderStyle.NONE);
                style.setBorderLeft(BorderStyle.NONE);
                style.setBorderTop(BorderStyle.NONE);
                style.setBorderBottom(BorderStyle.NONE);
            } else if (2 == cell.getColumnIndex()) {
                //第二列指标名称,居左
                style.setAlignment(HorizontalAlignment.LEFT);
            } else if (writeSheetHolder.getHead().size() - 1 == cell.getColumnIndex()) {
                //设置最后一列有边框
                style.setBorderRight(BorderStyle.DOUBLE);
                style.setRightBorderColor(IndexedColors.BLACK.getIndex());
            } else {

            }
            cell.setCellStyle(style);
        }
        //当前行
        int curRowIndex = cell.getRowIndex();
        //当前列
        int curColIndex = cell.getColumnIndex();
        if (curRowIndex > mergeRowIndex) {
            for (int columnIndex : mergeColumnIndex) {
                if (curColIndex == columnIndex) {
                    mergeWithPrevRow(writeSheetHolder, cell, curRowIndex, curColIndex);
                    break;
                }
            }
        }
        if (3 == curColIndex && curRowIndex >= 2) {
            mergeWithLeftLine(writeSheetHolder, cell, curRowIndex, curColIndex);
        }
    }


    private int[] mergeColumnIndex;
    private int mergeRowIndex;

    public Test(int mergeRowIndex, int[] mergeColumnIndex) {
        this.mergeRowIndex = mergeRowIndex;
        this.mergeColumnIndex = mergeColumnIndex;
    }


    private void mergeWithPrevRow(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
        //获取当前行的当前列的数据和上一行的当前列列数据，通过上一行数据是否相同进行合并
        Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
        Cell preCell = cell.getSheet().getRow(curRowIndex - 1).getCell(curColIndex);
        Object preData = preCell.getCellTypeEnum() == CellType.STRING ? preCell.getStringCellValue() : preCell.getNumericCellValue();

        // 比较当前行的第一列的单元格与上一行是否相同，相同合并当前单元格与上一行
        if (curData.equals(preData)) {
            Sheet sheet = writeSheetHolder.getSheet();
            List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
            boolean isMerged = false;
            for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
                CellRangeAddress cellRangeAddr = mergeRegions.get(i);
                // 若上一个单元格已经被合并，则先移出原有的合并单元，再重新添加合并单元
                if (cellRangeAddr.isInRange(curRowIndex - 1, curColIndex)) {
                    sheet.removeMergedRegion(i);
                    cellRangeAddr.setLastRow(curRowIndex);
                    sheet.addMergedRegion(cellRangeAddr);
                    isMerged = true;
                }
            }
            // 若上一个单元格未被合并，则新增合并单元
            if (!isMerged) {
                CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex - 1, curRowIndex, curColIndex, curColIndex);
                sheet.addMergedRegion(cellRangeAddress);
            }
        }
    }

    private void mergeWithLeftLine(WriteSheetHolder writeSheetHolder, Cell cell, int curRowIndex, int curColIndex) {
        //当前单元格中数据
        Object curData = cell.getCellTypeEnum() == CellType.STRING ? cell.getStringCellValue() : cell.getNumericCellValue();
        //获取当前单元格的左面一个单元格
        Cell leftCell = cell.getSheet().getRow(curRowIndex).getCell(curColIndex - 1);
        //获取当前单元格的左面一个单元格中的数据
        Object leftData = leftCell.getCellTypeEnum() == CellType.STRING ? leftCell.getStringCellValue() : leftCell.getNumericCellValue();

        // 将当前单元格数据与左侧一个单元格数据比较
        if (StringUtils.isEmpty((String) curData)) {
            //获取当前sheet页
            Sheet sheet = writeSheetHolder.getSheet();
            //得到所有的合并单元格
            List<CellRangeAddress> mergeRegions = sheet.getMergedRegions();
            //是否合并
            boolean isMerged = false;
            for (int i = 0; i < mergeRegions.size() && !isMerged; i++) {
                //CellRangeAddress POI合并单元格
                //CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol)
                //例子：CellRangeAddress（2, 6000, 3, 3）；
                //第2行起 第6000行终止 第3列开始 第3列结束。
                CellRangeAddress cellRangeAddr = mergeRegions.get(i);
                // cellRangeAddr.isInRange(int rowInd, int colInd)确定给定坐标是否在此范围的范围内。
                // 若左侧一个单元格已经被合并，则先移出原有的合并单元，再重新添加合并单元
                if (cellRangeAddr.isInRange(curRowIndex, curColIndex - 1)) {
                    sheet.removeMergedRegion(i);
                    cellRangeAddr.setLastColumn(curColIndex);
                    sheet.addMergedRegion(cellRangeAddr);
                    isMerged = true;
                }
            }
            // 若左侧一个单元格未被合并，则新增合并单元
            if (!isMerged) {
                CellRangeAddress cellRangeAddress = new CellRangeAddress(curRowIndex, curRowIndex, curColIndex - 1, curColIndex);
                sheet.addMergedRegion(cellRangeAddress);
            }
        }
    }


//    boolean islist = false;
//    ArrayList datas = null;
//    // 这就是上面ArrayListURLConverter的返回值,被塞到了cellDataList.get(0).getData中
//        if (null == cellDataList || null == cellDataList.get(0)) {
//        return;
//    }
//        if (cellDataList.get(0).getData() instanceof ArrayList) {
//        datas = (ArrayList) cellDataList.get(0).getData();
//        if (datas.get(0) instanceof CellData) {
//            CellData c = (CellData) datas.get(0);
//            if (c.getImageValue() == null) {
//                return;
//            } else {
//                islist = true;
//            }
//        }
//    }
//        if (!islist && cellDataList.get(0).getImageValue() == null) {
//        return;
//    }
//    String key = cell.getRowIndex() + "_" + cell.getColumnIndex();
//        if (repeats.contains(key)) { // afterCellDispose好像会被重复调用
//        return;
//    }
//        repeats.add(key);
//    // 这里默认要导出的图片大小为60*60px,60px的行高大约是900, 60px列宽大概是248*8,这个可以根据需求来调
////        sheet.getRow(cell.getRowIndex()).setHeight((short) 4785);
////        sheet.setColumnWidth(cell.getColumnIndex(), islist ? 16125 * datas.size() : 16125);
//        if (islist) {
//        for (int i = 0; i < datas.size(); i++) {
//            CellData cellData = (CellData) datas.get(i);
//            if (cellData.getImageValue() == null) {
//                continue;
//            }
//            this.insertImage(sheet, cell, cellData.getImageValue());
//        }
//    } else {
//        // cellDataList 是list的原因是 填充的情况下 可能会多个写到一个单元格 但是如果普通写入 一定只有一个
//        this.insertImage(sheet, cell, cellDataList.get(0).getImageValue());
//    }

    /**
     * 设置第一列样式
     * @param cellStyle
     * @return
     */
    private static CellStyle setFirstColStyle(CellStyle cellStyle) {
        return null;
    }


    private Workbook workbook;

    @Override
    protected void initCellStyle(Workbook workbook) {
        this.workbook = workbook;
    }

    @Override
    protected void setHeadCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        if (cell.getRowIndex() == 0) {
            // 设置字体
            Font font = workbook.createFont();
            //设置字体大小
            font.setFontHeightInPoints((short) 10);
            //字体加粗
            font.setBold(true);
            //设置字体名字
            font.setFontName("微软雅黑");
            //设置样式;
            CellStyle style = workbook.createCellStyle();
            //左右居中
            style.setAlignment(HorizontalAlignment.CENTER);
            //垂直居中
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            //设置边框
            style.setBorderBottom(BorderStyle.THIN);
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);
            style.setBorderTop(BorderStyle.THIN);
            //在样式用应用设置的字体;
            style.setFont(font);
            //设置自动换行;
            style.setWrapText(false);
            cell.setCellStyle(style);
        } else if (cell.getRowIndex() == 1) {
            // 设置字体
            Font font = workbook.createFont();
            //设置字体大小
            font.setFontHeightInPoints((short) 12);
            //字体加粗
            font.setBold(true);
            //设置字体名字
            font.setFontName("楷体");
            //设置样式;
            CellStyle style = workbook.createCellStyle();
            //左右居中
            style.setAlignment(HorizontalAlignment.CENTER);
            //垂直居中
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            //设置边框
            style.setBorderBottom(BorderStyle.THIN);
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);
            style.setBorderTop(BorderStyle.THIN);
            //在样式用应用设置的字体;
            style.setFont(font);
            //设置自动换行;
            style.setWrapText(false);
            cell.setCellStyle(style);
        }
        if (cell.getRowIndex() > 1) {
            CellStyle style = workbook.createCellStyle();
            //设置边框
            style.setBorderBottom(BorderStyle.THIN);
            style.setBorderLeft(BorderStyle.THIN);
            style.setBorderRight(BorderStyle.THIN);
            style.setBorderTop(BorderStyle.THIN);
            cell.setCellStyle(style);
        }
    }

    @Override
    protected void setContentCellStyle(Cell cell, Head head, Integer relativeRowIndex) {
        Font font = workbook.createFont();
        //设置字体大小
        font.setFontHeightInPoints((short) 12);

        CellStyle style = workbook.createCellStyle();
        //设置边框
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        style.setBorderTop(BorderStyle.THIN);
        style.setFont(font);
        cell.setCellStyle(style);
    }

    private void insertImage(Sheet sheet, Cell cell, byte[] pictureData) {
        int picWidth = Units.pixelToEMU(120);
        int index = sheet.getWorkbook().addPicture(pictureData, HSSFWorkbook.PICTURE_TYPE_PNG);
        Drawing drawing = sheet.getDrawingPatriarch();
        if (drawing == null) {
            drawing = sheet.createDrawingPatriarch();
        }

        CreationHelper helper = sheet.getWorkbook().getCreationHelper();
        ClientAnchor anchor = helper.createClientAnchor();
        // 设置图片坐标
        anchor.setDx1(picWidth);
        anchor.setDy1(picWidth);
        anchor.setDx2(picWidth);
        anchor.setDy2(picWidth);
        // 设置图片位置
        anchor.setCol1(cell.getColumnIndex());
        anchor.setCol2(cell.getColumnIndex());
        anchor.setRow1(cell.getRowIndex());
        anchor.setRow2(cell.getRowIndex());
        // 设置图片可以随着单元格移动
        anchor.setAnchorType(ClientAnchor.AnchorType.MOVE_AND_RESIZE);
        drawing.createPicture(anchor, index);
    }


}